package com.lbwnb;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {
    private MyDBHelper dbHelper;
    Button Login;
    Button Regist;
    EditText Username;
    EditText Password;
    EditText RePassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        dbHelper = new MyDBHelper(RegisterActivity.this, "userData.db", null, 1);
        initView();
        initData();
    }

    private void initData() {
        Regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //SQLiteDatabase db=dbHelper.getWritableDatabase();
                String newname = Username.getText().toString();
                String password = Password.getText().toString();
                String repassword = RePassword.getText().toString();
                if (newname.isEmpty() || password.isEmpty() || repassword.isEmpty()) {
                    Toast.makeText(RegisterActivity.this, "请输入完整内容", Toast.LENGTH_SHORT).show();
                } else if (!password.equals(repassword)) {
                    Toast.makeText(RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                } else if (CheckIsDataAlreadyInDBorNot(newname)) {
                    Toast.makeText(RegisterActivity.this, "该用户名已被注册，注册失败", Toast.LENGTH_SHORT).show();
                } else {
                    if (register(newname, password)) {
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                        startActivity(intent);
                        RegisterActivity.this.finish();
                    }
                }
            }
        });
        Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        Username = findViewById(R.id.et_username);
        Password = findViewById(R.id.et_password);
        RePassword = findViewById(R.id.et_repassword);
        Login = findViewById(R.id.bt_login);
        Regist = findViewById(R.id.bt_regist);
    }

    //向数据库插入数据
    public boolean register(String username, String password) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        /*String sql = "insert into userData(name,password) value(?,?)";
        Object obj[]={username,password};
        db.execSQL(sql,obj);*/
        ContentValues values = new ContentValues();
        values.put("name", username);
        values.put("password", password);
        db.insert("userData", null, values);
        db.close();
        //db.execSQL("insert into userData (name,password) values (?,?)",new String[]{username,password});
        return true;
    }

    //检验用户名是否已存在
    public boolean CheckIsDataAlreadyInDBorNot(String value) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String Query = "Select * from userData where name =?";
        Cursor cursor = db.rawQuery(Query, new String[]{value});
        if (cursor.getCount() > 0) {
            cursor.close();
            return true;
        }
        cursor.close();
        return false;
    }

}